<?php

/**
 * This is the model class for table "work_order_line_items".
 *
 * The followings are the available columns in table 'work_order_line_items':
 * @property integer $work_order_id
 * @property integer $item_id
 * @property integer $quantity_required
 * @property integer $quantity_received
 * @property integer $balace
 */
class WorkOrderLineItem extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return WorkOrderLineItem the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'work_order_line_items';
	}
	
	public function primaryKey()
	{
		return 'work_order_id';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('work_order_id, item_id', 'required'),
			array('work_order_id, item_id, quantity_required, quantity_received, balance', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('work_order_id, item_id, quantity_required, quantity_received, balance', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'item' => array(self::BELONGS_TO, 'Item', 'item_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'work_order_id' => 'Work Order',
			'item_id' => 'Item',
			'quantity_required' => 'Quantity Required',
			'quantity_received' => 'Quantity Received',
			'balance' => 'Balace',
		);
	}
	
	public function getJsonDefaultLineItems($woId=null, $minimumLines=3)
	{

		if ($woId != null)
		{
			$line_items_array = self::findLineItems($woId);
			
			$line_items = array();
			
			foreach($line_items_array as $line_item)
			{
				$line_items['item_id'][] = $line_item->item->id;
				$line_items['item_code'][] = $line_item->item->code;
				$line_items['description'][] = $line_item->item->short_description;
				$line_items['qnty_required'][] = $line_item->quantity_required;
				$line_items['qnty_received'][] = $line_item->quantity_received;
				$line_items['balance'][] =  $line_item->balance;
			}
			
		}
		else {
			
			for ($i=0;$i<$minimumLines;$i++)
			{
				$line_items['item_id'][] = '';
				$line_items['item_code'][] = '';
				$line_items['description'][] = '';
				$line_items['qnty_required'][] =0;
				$line_items['qnty_received'][] = 0;
				$line_items['balance'][] = 0;
			}
			
		}
		
		return json_encode($line_items);
	}
	
	public function findLineItems($woId)
	{
		$line_items = self::model()->findAllByPk($woId);
		return $line_items;
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('work_order_id',$this->work_order_id);
		$criteria->compare('item_id',$this->item_id);
		$criteria->compare('quantity_required',$this->quantity_required);
		$criteria->compare('quantity_received',$this->quantity_received);
		$criteria->compare('balace',$this->balace);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}